﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Dotnet.Utils.Utility.DrawingUtil
{
    public class ImageUtil
    {
        private ImageUtil()
        {
        }

        public static Size GetScaleSize(Size szSrc, Size szDes)
        {
            Size result;
            if (szSrc.Width == 0 || szSrc.Height == 0)
            {
                result = szSrc;
            }
            else if (szSrc.Width <= szDes.Width && szSrc.Height <= szDes.Height)
            {
                result = szSrc;
            }
            else
            {
                int num = szSrc.Width - szDes.Width;
                int num2 = szSrc.Height - szDes.Height;
                float num3;
                if (num > num2)
                {
                    num3 = num / (float)szSrc.Width;
                }
                else
                {
                    num3 = num2 / (float)szSrc.Height;
                }
                int width = szSrc.Width - (int)(szSrc.Width * num3);
                int height = szSrc.Height - (int)(szSrc.Height * num3);
                result = new Size(width, height);
            }
            return result;
        }

        public static byte[] GetReducedImageData(byte[] byteImage, int iFinalWidth, int iFinalHeight)
        {
            MemoryStream memoryStream = new MemoryStream(byteImage, 0, byteImage.Length);
            Bitmap bitmap = new Bitmap(memoryStream);
            byte[] result;
            if (bitmap.Width <= iFinalWidth && bitmap.Height <= iFinalHeight)
            {
                bitmap.Dispose();
                memoryStream.Close();
                result = byteImage;
            }
            else
            {
                Size scaleSize = GetScaleSize(new Size(bitmap.Width, bitmap.Height), new Size(iFinalWidth, iFinalHeight));
                int width = scaleSize.Width;
                int height = scaleSize.Height;
                Bitmap bitmap2 = new Bitmap(width, height);
                Graphics graphics = Graphics.FromImage(bitmap2);
                graphics.DrawImage(bitmap, 0, 0, width, height);
                bitmap.Dispose();
                memoryStream.Close();
                MemoryStream memoryStream2 = new MemoryStream();
                bitmap2.Save(memoryStream2, ImageFormat.Jpeg);
                byte[] array = memoryStream2.ToArray();
                bitmap2.Dispose();
                memoryStream2.Close();
                result = array;
            }
            return result;
        }

        public static void CutForSquare(Stream fromFile, string fileSaveUrl, int side, int quality)
        {
            string directoryName = Path.GetDirectoryName(fileSaveUrl);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            Image image = Image.FromStream(fromFile, true);
            if (image.Width <= side && image.Height <= side)
            {
                image.Save(fileSaveUrl, ImageFormat.Jpeg);
            }
            else
            {
                int num = image.Width;
                int num2 = image.Height;
                if (num != num2)
                {
                    Image image2;
                    Graphics graphics;
                    if (num > num2)
                    {
                        image2 = new Bitmap(num2, num2);
                        graphics = Graphics.FromImage(image2);
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        Rectangle srcRect = new Rectangle((num - num2) / 2, 0, num2, num2);
                        Rectangle destRect = new Rectangle(0, 0, num2, num2);
                        graphics.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
                        num = num2;
                    }
                    else
                    {
                        image2 = new Bitmap(num, num);
                        graphics = Graphics.FromImage(image2);
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        Rectangle srcRect = new Rectangle(0, (num2 - num) / 2, num, num);
                        Rectangle destRect = new Rectangle(0, 0, num, num);
                        graphics.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
                        num2 = num;
                    }
                    image = (Image)image2.Clone();
                    graphics.Dispose();
                    image2.Dispose();
                }
                Image image3 = new Bitmap(side, side);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, side, side), new Rectangle(0, 0, num, num2), GraphicsUnit.Pixel);
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo encoder = null;
                foreach (ImageCodecInfo imageCodecInfo in imageEncoders)
                {
                    if (imageCodecInfo.MimeType == "image/jpeg" || imageCodecInfo.MimeType == "image/bmp" || imageCodecInfo.MimeType == "image/png" || imageCodecInfo.MimeType == "image/gif")
                    {
                        encoder = imageCodecInfo;
                    }
                }
                EncoderParameters encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                image3.Save(fileSaveUrl, encoder, encoderParameters);
                encoderParameters.Dispose();
                graphics2.Dispose();
                image3.Dispose();
                image.Dispose();
            }
        }

        public static void CutForCustom(Stream fromFile, string fileSaveUrl, int maxWidth, int maxHeight, int quality)
        {
            Image image = Image.FromStream(fromFile, true);
            if (image.Width <= maxWidth && image.Height <= maxHeight)
            {
                image.Save(fileSaveUrl, ImageFormat.Jpeg);
            }
            else
            {
                double num = maxWidth / (double)maxHeight;
                double num2 = image.Width / (double)image.Height;
                if (num == num2)
                {
                    Image image2 = new Bitmap(maxWidth, maxHeight);
                    Graphics graphics = Graphics.FromImage(image2);
                    graphics.InterpolationMode = InterpolationMode.High;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.Clear(Color.White);
                    graphics.DrawImage(image, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                    image2.Save(fileSaveUrl, ImageFormat.Jpeg);
                }
                else
                {
                    Rectangle srcRect = new Rectangle(0, 0, 0, 0);
                    Rectangle destRect = new Rectangle(0, 0, 0, 0);
                    Image image3;
                    Graphics graphics2;
                    if (num > num2)
                    {
                        image3 = new Bitmap(image.Width, (int)Math.Floor(image.Width / num));
                        graphics2 = Graphics.FromImage(image3);
                        srcRect.X = 0;
                        srcRect.Y = (int)Math.Floor((image.Height - image.Width / num) / 2.0);
                        srcRect.Width = image.Width;
                        srcRect.Height = (int)Math.Floor(image.Width / num);
                        destRect.X = 0;
                        destRect.Y = 0;
                        destRect.Width = image.Width;
                        destRect.Height = (int)Math.Floor(image.Width / num);
                    }
                    else
                    {
                        image3 = new Bitmap((int)Math.Floor(image.Height * num), image.Height);
                        graphics2 = Graphics.FromImage(image3);
                        srcRect.X = (int)Math.Floor((image.Width - image.Height * num) / 2.0);
                        srcRect.Y = 0;
                        srcRect.Width = (int)Math.Floor(image.Height * num);
                        srcRect.Height = image.Height;
                        destRect.X = 0;
                        destRect.Y = 0;
                        destRect.Width = (int)Math.Floor(image.Height * num);
                        destRect.Height = image.Height;
                    }
                    graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics2.SmoothingMode = SmoothingMode.HighQuality;
                    graphics2.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
                    Image image2 = new Bitmap(maxWidth, maxHeight);
                    Graphics graphics = Graphics.FromImage(image2);
                    graphics.InterpolationMode = InterpolationMode.High;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.Clear(Color.White);
                    graphics.DrawImage(image3, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, image3.Width, image3.Height), GraphicsUnit.Pixel);
                    ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo encoder = null;
                    foreach (ImageCodecInfo imageCodecInfo in imageEncoders)
                    {
                        if (imageCodecInfo.MimeType == "image/jpeg" || imageCodecInfo.MimeType == "image/bmp" || imageCodecInfo.MimeType == "image/png" || imageCodecInfo.MimeType == "image/gif")
                        {
                            encoder = imageCodecInfo;
                        }
                    }
                    EncoderParameters encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                    image2.Save(fileSaveUrl, encoder, encoderParameters);
                    graphics.Dispose();
                    image2.Dispose();
                    graphics2.Dispose();
                    image3.Dispose();
                }
            }
            image.Dispose();
        }

        public static Image AddWaterMarkText(Image img, string watermarkText, float fontSize = 20f)
        {
            return AddWaterMarkText(img, watermarkText, Color.Black, fontSize);
        }

        public static Image AddWaterMarkText(Image img, string watermarkText, Color color, float fontSize = 20f)
        {
            if (!string.IsNullOrEmpty(watermarkText))
            {
                if (IsPixelFormatIndexed(img.PixelFormat))
                {
                    Bitmap bitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.CompositingQuality = CompositingQuality.HighQuality;
                        graphics.DrawImage(img, 0, 0);
                        graphics.Save();
                    }
                    img = bitmap;
                }
                using (Graphics graphics2 = Graphics.FromImage(img))
                {
                    Font font = new Font("黑体", fontSize, GraphicsUnit.Pixel);
                    Brush brush = new SolidBrush(color);
                    int num = watermarkText.Length * 10 + 10;
                    int num2 = 10;
                    if (img.Width > num)
                    {
                        num2 = img.Width - num;
                    }
                    int num3 = 5;
                    if (img.Height > 25)
                    {
                        num3 = img.Height - 25;
                    }
                    graphics2.DrawString(watermarkText, font, brush, num2, num3);
                    graphics2.Save();
                    graphics2.Dispose();
                }
            }
            return img;
        }

        public static Image AddWaterMarkText(Image img, string watermarkText, int pos2Right, int pos2Bottom, float fontSize = 20f)
        {
            return AddWaterMarkText(img, watermarkText, Color.Black, pos2Right, pos2Bottom, fontSize);
        }

        public static Image AddWaterMarkText(Image img, string watermarkText, Color color, int pos2Right, int pos2Bottom, float fontSize = 20f)
        {
            if (!string.IsNullOrEmpty(watermarkText))
            {
                if (IsPixelFormatIndexed(img.PixelFormat))
                {
                    Bitmap bitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.CompositingQuality = CompositingQuality.HighQuality;
                        graphics.DrawImage(img, 0, 0);
                        graphics.Save();
                    }
                    img = bitmap;
                }
                using (Graphics graphics2 = Graphics.FromImage(img))
                {
                    Font font = new Font("黑体", fontSize, GraphicsUnit.Pixel);
                    Brush brush = new SolidBrush(color);
                    int num = watermarkText.Length * 25;
                    int num2 = 0;
                    if (img.Width > pos2Right)
                    {
                        num2 = img.Width - pos2Right;
                    }
                    int num3 = 5;
                    if (img.Height > pos2Bottom)
                    {
                        num3 = img.Height - pos2Bottom;
                    }
                    graphics2.DrawString(watermarkText, font, brush, num2, num3);
                    graphics2.Save();
                    graphics2.Dispose();
                }
            }
            return img;
        }

        private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
        {
            foreach (PixelFormat pixelFormat in indexedPixelFormats)
            {
                if (pixelFormat.Equals(imgPixelFormat))
                {
                    return true;
                }
            }
            return false;
        }

        public static Image AddWaterMarkImage(Image img, string watermarkImage)
        {
            if (!string.IsNullOrEmpty(watermarkImage))
            {
                if (File.Exists(watermarkImage))
                {
                    if (IsPixelFormatIndexed(img.PixelFormat))
                    {
                        Bitmap bitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.SmoothingMode = SmoothingMode.HighQuality;
                            graphics.CompositingQuality = CompositingQuality.HighQuality;
                            graphics.DrawImage(img, 0, 0);
                            graphics.Save();
                        }
                        img = bitmap;
                    }
                    using (Image image = Image.FromFile(watermarkImage))
                    {
                        if (img.Width >= image.Width && img.Height >= image.Height)
                        {
                            Graphics graphics2 = Graphics.FromImage(img);
                            ImageAttributes imageAttributes = new ImageAttributes();
                            ColorMap[] map = new ColorMap[]
                            {
                                new ColorMap
                                {
                                    OldColor = Color.FromArgb(255, 0, 255, 0),
                                    NewColor = Color.FromArgb(0, 0, 0, 0)
                                }
                            };
                            imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                            float[][] array = new float[5][];
                            float[][] array2 = array;
                            int num = 0;
                            float[] array3 = new float[5];
                            array3[0] = 1f;
                            array2[num] = array3;
                            float[][] array4 = array;
                            int num2 = 1;
                            array3 = new float[5];
                            array3[1] = 1f;
                            array4[num2] = array3;
                            float[][] array5 = array;
                            int num3 = 2;
                            array3 = new float[5];
                            array3[2] = 1f;
                            array5[num3] = array3;
                            float[][] array6 = array;
                            int num4 = 3;
                            array3 = new float[5];
                            array3[3] = 0.5f;
                            array6[num4] = array3;
                            array[4] = new float[]
                            {
                                0f,
                                0f,
                                0f,
                                0f,
                                1f
                            };
                            float[][] newColorMatrix = array;
                            ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                            imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                            graphics2.DrawImage(image, new Rectangle(img.Width - image.Width, img.Height - image.Height, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
                            graphics2.Save();
                            graphics2.Dispose();
                        }
                        image.Dispose();
                    }
                }
            }
            return img;
        }

        public static void ZoomAuto(Stream fromFile, string savePath, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            string directoryName = Path.GetDirectoryName(savePath);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            Image image = Image.FromStream(fromFile, true);
            if (image.Width <= targetWidth && image.Height <= targetHeight)
            {
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image))
                    {
                        Font font = new Font("黑体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (!string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image.Width >= image2.Width && image.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array2 = array;
                                int num = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array2[num] = array3;
                                float[][] array4 = array;
                                int num2 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array4[num2] = array3;
                                float[][] array5 = array;
                                int num3 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array5[num3] = array3;
                                float[][] array6 = array;
                                int num4 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array6[num4] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                image.Save(savePath, imgFormat);
            }
            else
            {
                double num5 = image.Width;
                double num6 = image.Height;
                if (image.Width > image.Height || image.Width == image.Height)
                {
                    if (image.Width > targetWidth)
                    {
                        num5 = targetWidth;
                        num6 = image.Height * (targetWidth / image.Width);
                    }
                }
                else if (image.Height > targetHeight)
                {
                    num6 = targetHeight;
                    num5 = image.Width * (targetHeight / image.Height);
                }
                Image image3 = new Bitmap((int)num5, (int)num6);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, image3.Width, image3.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image3))
                    {
                        Font font = new Font("宋体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image3.Width >= image2.Width && image3.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image3);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array7 = array;
                                int num7 = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array7[num7] = array3;
                                float[][] array8 = array;
                                int num8 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array8[num8] = array3;
                                float[][] array9 = array;
                                int num9 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array9[num9] = array3;
                                float[][] array10 = array;
                                int num10 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array10[num10] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image3.Width - image2.Width, image3.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                image3.Save(savePath, imgFormat);
                graphics2.Dispose();
                image3.Dispose();
                image.Dispose();
            }
        }

        public static Image ZoomAuto(Stream fromFile, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            Image image = Image.FromStream(fromFile, true);
            Image result;
            if (image.Width <= targetWidth && image.Height <= targetHeight)
            {
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image))
                    {
                        Font font = new Font("黑体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (!string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image.Width >= image2.Width && image.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array2 = array;
                                int num = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array2[num] = array3;
                                float[][] array4 = array;
                                int num2 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array4[num2] = array3;
                                float[][] array5 = array;
                                int num3 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array5[num3] = array3;
                                float[][] array6 = array;
                                int num4 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array6[num4] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                result = image;
            }
            else
            {
                double num5 = image.Width;
                double num6 = image.Height;
                if (image.Width > image.Height || image.Width == image.Height)
                {
                    if (image.Width > targetWidth)
                    {
                        num5 = targetWidth;
                        num6 = image.Height * (targetWidth / image.Width);
                    }
                }
                else if (image.Height > targetHeight)
                {
                    num6 = targetHeight;
                    num5 = image.Width * (targetHeight / image.Height);
                }
                Image image3 = new Bitmap((int)num5, (int)num6);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, image3.Width, image3.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                if (!string.IsNullOrEmpty(watermarkText))
                {
                    using (Graphics graphics = Graphics.FromImage(image3))
                    {
                        Font font = new Font("宋体", 10f);
                        Brush brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                        graphics.Save();
                        graphics.Dispose();
                    }
                }
                if (string.IsNullOrEmpty(watermarkImage))
                {
                    if (File.Exists(watermarkImage))
                    {
                        using (Image image2 = Image.FromFile(watermarkImage))
                        {
                            if (image3.Width >= image2.Width && image3.Height >= image2.Height)
                            {
                                Graphics graphics = Graphics.FromImage(image3);
                                ImageAttributes imageAttributes = new ImageAttributes();
                                ColorMap[] map = new ColorMap[]
                                {
                                    new ColorMap
                                    {
                                        OldColor = Color.FromArgb(255, 0, 255, 0),
                                        NewColor = Color.FromArgb(0, 0, 0, 0)
                                    }
                                };
                                imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                                float[][] array = new float[5][];
                                float[][] array7 = array;
                                int num7 = 0;
                                float[] array3 = new float[5];
                                array3[0] = 1f;
                                array7[num7] = array3;
                                float[][] array8 = array;
                                int num8 = 1;
                                array3 = new float[5];
                                array3[1] = 1f;
                                array8[num8] = array3;
                                float[][] array9 = array;
                                int num9 = 2;
                                array3 = new float[5];
                                array3[2] = 1f;
                                array9[num9] = array3;
                                float[][] array10 = array;
                                int num10 = 3;
                                array3 = new float[5];
                                array3[3] = 0.5f;
                                array10[num10] = array3;
                                array[4] = new float[]
                                {
                                    0f,
                                    0f,
                                    0f,
                                    0f,
                                    1f
                                };
                                float[][] newColorMatrix = array;
                                ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                                imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                graphics.DrawImage(image2, new Rectangle(image3.Width - image2.Width, image3.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes);
                                graphics.Save();
                                graphics.Dispose();
                            }
                            image2.Dispose();
                        }
                    }
                }
                graphics2.Dispose();
                image.Dispose();
                result = image3;
            }
            return result;
        }

        public static Bitmap BuildCheckCodePicture(out string checkCodeString)
        {
            string text = string.Empty;
            Color[] array = new Color[]
            {
                Color.Black,
                Color.Red,
                Color.Blue,
                Color.Green,
                Color.Orange,
                Color.Brown,
                Color.Brown,
                Color.DarkBlue
            };
            string[] array2 = new string[]
            {
                "Times New Roman",
                "MS Mincho",
                "Book Antiqua",
                "Gungsuh",
                "PMingLiU",
                "Impact"
            };
            char[] array3 = new char[]
            {
                '2',
                '3',
                '4',
                '5',
                '6',
                '8',
                '9',
                'A',
                'B',
                'C',
                'D',
                'E',
                'F',
                'G',
                'H',
                'J',
                'K',
                'L',
                'M',
                'N',
                'P',
                'R',
                'S',
                'T',
                'W',
                'X',
                'Y'
            };
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                text += array3[random.Next(array3.Length)];
            }
            checkCodeString = text;
            Bitmap bitmap = new Bitmap(100, 40);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            for (int i = 0; i < 10; i++)
            {
                int x = random.Next(100);
                int y = random.Next(40);
                int x2 = random.Next(100);
                int y2 = random.Next(40);
                Color color = array[random.Next(array.Length)];
                graphics.DrawLine(new Pen(color), x, y, x2, y2);
            }
            for (int i = 0; i < text.Length; i++)
            {
                string familyName = array2[random.Next(array2.Length)];
                Font font = new Font(familyName, 18f);
                Color color = array[random.Next(array.Length)];
                graphics.DrawString(text[i].ToString(), font, new SolidBrush(color), i * 20f + 8f, 8f);
            }
            for (int i = 0; i < 100; i++)
            {
                int x3 = random.Next(bitmap.Width);
                int y3 = random.Next(bitmap.Height);
                Color color = array[random.Next(array.Length)];
                bitmap.SetPixel(x3, y3, color);
            }
            return bitmap;
        }

        public static Stream BuildCheckCodeStream(out string checkCodeString)
        {
            Bitmap bitmap = BuildCheckCodePicture(out checkCodeString);
            MemoryStream memoryStream = new MemoryStream();
            try
            {
                bitmap.Save(memoryStream, ImageFormat.Png);
            }
            finally
            {
                bitmap.Dispose();
            }
            return memoryStream;
        }

        public static bool IsWebImage(string contentType)
        {
            return contentType == "image/pjpeg" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/bmp" || contentType == "image/png";
        }

        private static PixelFormat[] indexedPixelFormats = new PixelFormat[]
        {
            PixelFormat.Undefined,
            PixelFormat.Undefined,
            PixelFormat.Format16bppArgb1555,
            PixelFormat.Format1bppIndexed,
            PixelFormat.Format4bppIndexed,
            PixelFormat.Format8bppIndexed
        };
    }
}